package com.leetcode;

import com.leetcode.common.ListNode;

/**
 * 61. 旋转链表
 * 闭合为环
 *
 * @author fy
 * @date 2022/4/11 18:38
 */
public class Solution61_1 {

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k <= 0) {
            return head;
        }
        int length = 1;
        ListNode cur = head;
        while (cur.next != null) {
            length++;
            cur = cur.next;
        }
        int add = length - k % length;
        if (add == length) {
            return head;
        }
        cur.next = head;
        while (add > 0) {
            cur = cur.next;
            add--;
        }
        ListNode ret = cur.next;
        cur.next = null;
        return ret;
    }
}
